<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css" />
</head>

<body>

    <body class="pear-container">
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="mgclb-table" lay-filter="mgclb-table"></table>
            </div>
        </div>
    </body>

    <script type="text/html" id="mgclb-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>
		</script>

    <script type="text/html" id="mgclb-bar">
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">删除</button>
	</script>


    <script src="../../component/layui/layui.js"></script>
    <script src="../../component/pear/pear.js"></script>
    <script>
        layui.use(['table', 'form', 'jquery'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;

            let MODULE_PATH = "/system/user/";

            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '序号',
                    field: 'id',
                    align: 'center',
                    width: 100
                },
                {
                    title: '敏感词',
                    field: 'word',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#mgclb-bar',
                    align: 'center',
                    width: 130
                }
                ]
            ]

            table.render({
                elem: '#mgclb-table',
                url: '/api/sensitive/list',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#mgclb-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('tool(mgclb-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            });

            table.on('toolbar(mgclb-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                }
            });

            form.on('submit(mgclb-query)', function (data) {
                table.reload('mgclb-table', {
                    where: data.field
                })
                return false;
            });

            form.on('switch(mgclb-enable)', function (obj) {
                layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
            });

            window.add = function () {
                layer.open({
						type: 2,
						title: '新增',
						shade: 0.1,
						area: ['500px', '180px'],
						content: 'mgclb_add.html'
					});
            }

            window.edit = function (obj) {
                layer.msg("修改");
            }

            window.remove = function (obj) {
                layer.confirm('确定要删除吗？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                  
                    let loading = layer.load();  
                    $.ajax({
                        url: '/api/sensitive/remove/' + obj.data.id,
                        dataType: 'json',
                        success: function (result) {
                            layer.close(loading);
                            if (result.msg == "删除成功") {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 500
                                }, function () {
                                    table.reload('mgclb-table');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 500
                                });
                            }
                        }
                    })
                });
            }

            window.batchRemove = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = "";
                for (let i = 0; i < data.length; i++) {
                    ids += data[i].id + ",";
                }
                ids = ids.substr(0, ids.length - 1);
                console.log(ids);
                layer.confirm('确定要删除已选中数据吗？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "/api/sensitive/batchRemove/" + ids,
                        dataType: 'json',
                        success: function (result) {
                            layer.close(loading);
                            if (result.msg == "删除成功") {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 500
                                }, function () {
                                    table.reload('mgclb-table');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 500
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function (param) {
                table.reload('mgclb-table');
            }
        })
    </script>
</body>

</html>